import Vue from 'vue'


//返回公共路径
export const sameURL = () => {
  // return "/api/jrj_itm" //dev
  return ".." //build

}

//载入meta
export const loadMeta = function (meta) {
  let url = sameURL() + "/design/loadClassJson?name=" + meta;
  let loadMeta = Vue.http.get(url);
  return loadMeta;
}

/*
获取查询url的参数
*/
export const getQueryString = function (key) {
  var value = "";
  ///获取当前页面的URL
  var sURL = window.document.URL;
  ///URL中是否包含查询字符串
  if (sURL.indexOf("?") > 0) {
    //分解URL,第二的元素为完整的查询字符串
    //即arrayParams[1]的值为【id=1&action=2】
    var arrayParams = sURL.split("?");
    //分解查询字符串
    //arrayURLParams[0]的值为【id=1 】
    //arrayURLParams[2]的值为【action=add】
    var arrayURLParams = arrayParams[1].split("&");

    //遍历分解后的键值对
    for (var i = 0; i < arrayURLParams.length; i++) {
      //分解一个键值对
      var sParam = arrayURLParams[i].split("=");
      if ((sParam[0] == key) && (sParam[1] != "")) {
        //找到匹配的的键,且值不为空
        value = sParam[1];
        break;
      }
    }
  }
  return value;
}


//取人员角色
export const getRole = function (userInfo) {
  let url = sameURL() + "/app/invoke?fun=QueryRelatedGraphNode"
  let data = {
    "entity": userInfo,
    "classRole": "SysPersonWithRole"
  }
  let getRole = Vue.http.post(url, data)
  return getRole
}

// 本地存
export const set = (key, value) => {
  let time = new Date().getTime();
  localStorage.setItem(key, JSON.stringify({
    'data': value,
    'time': time
  }));
}


//本地取
export const get = (key, exp) => {
  let nowtime = new Date().getTime();
  let msg = JSON.parse(localStorage.getItem(key));
  if (msg == null || (nowtime - msg.time) > exp) {
    return null
  } else {
    let ret = msg.data;
    return ret
  }
}


//载入数据
export const loadData = function (person, project, filters) {
  let url = sameURL() + "/app/invoke?fun=QueryObject";
  let param = {
    "appName": "projectManagement",
    "connectionId": 9,
    "factory": "7000",
    "filters": filters,
    "orderItems": null,
    "service": "JRJ_ITM",
    "type": project,
    "user": person
  };
  let loadData = Vue.http.post(url, param);
  return loadData;
}

//调用batchdo,新增一条数据
export const addList = function (project, properties) {
  let url = sameURL() + "/jridgeadvanced/batchdo";
  let graph5 = {
    "roots": [{
      "entity": {
        "id": 0,
        "type": project,
        "properties": properties,
      },
      "nodeId": 0,
      "children": [],
      "strategy": 129
    }]
  };
  let addList = Vue.http.post(url, graph5);
  return addList;
}


//调用batchdo,删除一条数据
export const deleteList = function (id, project) {
  let url6 = sameURL() + "/jridgeadvanced/batchdo";
  let graph6 = {
    "roots": [{
      "entity": {
        "id": id,
        "type": project,
        "properties": {},
      },
      "nodeId": 0,
      "children": [],
      "strategy": 132
    }]
  };
  let deleteList = Vue.http.post(url6, graph6);
  return deleteList;
}

//调用batchdo,更新一条数据
export const updateList = function (id, project, properties) {
  let url = sameURL() + "/jridgeadvanced/batchdo";
  let graph = {
    "roots": [{
      "entity": {
        "id": id,
        "type": project,
        "properties": properties,
      },
      "nodeId": 0,
      "children": [],
      "strategy": 130
    }]
  };
  let updateList = Vue.http.post(url, graph);
  return updateList;
}


//执行sql语句，通用！！！
export const doSQL = function (sql) {
  let url = sameURL() + '/app/executesql';
  let data = {
    "appname": "projectManagement",
    "sql": sql
  }
  let doSQL = Vue.http.post(url, data);
  return doSQL
}

